home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_07_01 / v7n1075a.txt < prev    next >
Text File  |  1989-02-13  |  4KB  |  137 lines

  1.  
  2.  
  3. ABBREV   OPERATORS          DESCRIPTION OF EXCEPTION CASES
  4. ------   --------------     -----------------------------------------------
  5.  
  6. OOB      a[n]  a<<n  a>>n   "Out Of Bounds" subscript or shift count
  7.  
  8. BAD_P    *p    p[n]  p->m   "Bad pointer" -- out-of-bounds, dangling, or
  9.                             null pointer
  10.  
  11. OFLO     a*b   a+b   a-b    "Overflow" -- a signed-arithmetic or pointer
  12.          ++a   a++          expression overflow
  13.          --a   a--
  14.  
  15. UFLO     a*b   a/b          "Underflow" -- a floating-point expression
  16.          a+b   a-b          result too small to represent
  17.  
  18. CHOP     a=b   f(a)         "Chop" or "Truncation" -- assignment, or
  19.                             calling prototyped-function, that loses bits
  20.  
  21. DIV_0    a/b   a%b          "Divide-by-zero"
  22.  
  23. XALIGN   (ptr *)            "Mis-alignment" -- a pointer cast that loses
  24.                             alignment information
  25.  
  26.  
  27.  
  28.  
  29.  1  /* strcmp - compare (unsigned) strings  */
  30.  2  int strcmp(
  31.  3      register const char s[],  /* : string */
  32.  4      register const char t[])  /* : string */
  33.  5      {
  34.  6      typedef unsigned char uchar;
  35.  7
  36.  8      while (*s != '\0' && *s == *t)
  37.  9          {
  38. 10          ++s;
  39. 11          ++t;
  40. 12          }
  41. 13      if (*(uchar *)s < *(uchar *)t))
  42. 14          return -1;
  43. 15      else if (*(uchar *)s == *(uchar *)t)
  44. 16          return 0;
  45. 17      else
  46. 18          return 1;
  47. 19      }
  48.  
  49.  
  50.  
  51.  
  52. LIST OF TEST CASES    PROGRAM LISTING                          EXCEPTIONS?
  53. ------------------    -------------------------------------    -----------
  54. ___F   ___TF  ___TT   8   while (*s != '\0' && *s == *t)         ___BAD_P
  55.                       9       {
  56.                      10       ++s;                               ___OFLO
  57.                      11       ++t;                               ___OFLO
  58.                      12       }
  59. ___F       ___T      13   if (*(uchar *)s < *(uchar *)t))        ___XALIGN
  60. ___MIN *s  ___MAX *t 14       return -1;
  61. ___F       ___T      15   else if (*(uchar *)s == *(uchar *)t)
  62.                      16       return 0;
  63.                      17   else
  64. ___MAX *s  ___MIN *t 18       return 1;
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. REVIEWER'S NOTES    SPECIFICATION
  72. ----------------    --------------------------------------------
  73.                     Outcomes of returned value:
  74. _1_                   Less than zero:    s compares low to t
  75. _2_                   Zero:              s compares equal to t
  76. _3_                   Greater than zero: s compares high to t
  77.                     Parameters  s  and  t  must point to strings,
  78. ___                   properly nul-terminated.
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. TEST CASES AND DESCRIPTIONS
  86. -------------------------------------------------------------
  87.  
  88. 1.  s is null string, t starts with largest unsigned char value
  89. 2.  s and t are equal strings, e.g.  "a"
  90. 3.  s starts with largest unsigned char value, t is null string
  91.  
  92.  
  93.  
  94.  
  95. GUARANTEES AGAINST EXCEPTIONS
  96. -------------------------------------------------------------
  97. 4.  Specification: strings s and t are null-terminated,
  98.     so the pointer increments cannot overflow.
  99. 5.  C Standard: unsigned char has the same alignment as char.
  100.  
  101.  
  102.  
  103.  
  104. LIST OF TEST CASES    PROGRAM LISTING                          EXCEPTIONS?
  105. ------------------    -------------------------------------    -----------
  106. _1_F   _3_TF  _2_TT   8   while (*s != '\0' && *s == *t)         _4_BAD_P
  107.                       9       {
  108.                      10       ++s;                               _4_OFLO
  109.                      11       ++t;                               _4_OFLO
  110.                      12       }
  111. _3_F       _1_T      13   if (*(uchar *)s < *(uchar *)t))        _5_XALIGN
  112. _1_MIN *s  _1_MAX *t 14       return -1;
  113. _3_F       _2_T      15   else if (*(uchar *)s == *(uchar *)t)
  114.                      16       return 0;
  115.                      17   else
  116. _3_MAX *s  _3_MIN *t 18       return 1;
  117.  
  118.  
  119.  
  120.  
  121.  
  122. TEST PROGRAM
  123. -------------------------------------------------------------
  124. #include <limits.h>
  125. #include <assert.h>
  126. #undef NDEBUG   /* turn on assert's */
  127. char s_max[2] = {UCHAR_MAX, '\0'};  /* string with largest uchar */
  128. main()
  129.     {
  130.     assert(strcmp("", s_max) < 0);      /* case 1 */
  131.     assert(strcmp("a", "a") == 0);      /* case 2 */
  132.     assert(strcmp(s_max, "") > 0);      /* case 3 */
  133.     }
  134.  
  135.  
  136.  
  137.